package com.netty.esc.controller;

import com.alibaba.fastjson.JSONObject;
import com.netty.esc.Interceptor.annotation.ControllerMapping;
import com.netty.esc.Interceptor.annotation.FuckAll;
import com.netty.esc.common.base.HttpStatusCodeEM;
import com.netty.esc.common.base.BaseResponseDTO;
import com.netty.esc.service.http.SendSMS;
import com.netty.esc.common.enums.SMSEnum;
import com.netty.esc.entity.model.UserInfoDTO;
import com.netty.esc.entity.pojo.UserInfo;
import com.netty.esc.service.http.UserService;
import com.netty.esc.utils.TokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@FuckAll
@ControllerMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @Resource
    TokenUtil tokenUtil;

    @Resource
    SendSMS sendSMS;


    @PostMapping("/login")
    @ResponseBody
    public BaseResponseDTO<?> login(@RequestBody UserInfoDTO userInfoDTO){
        UserInfo userLoginMessage = userService.getUserLoginMessage(userInfoDTO);
        if (Objects.isNull(userLoginMessage)){
            return BaseResponseDTO.setResultError(HttpStatusCodeEM.PASSWORD_ERROR,"登录失败，请检查手机号或密码是否正确");
        }
        //登录成功
        String token = tokenUtil.makeTokenInRedis(userInfoDTO);
        //token写入redis成功
        if (!StringUtils.isBlank(token)){
            Map<String,String> map = new HashMap<>();
            map.put("token",token);
            return BaseResponseDTO.setResultSuccess(map);
        }else {
            return BaseResponseDTO.setResultError("登录失败，token生成或者写入失败...请稍后重试");
        }
    }

    /**
     * 注册接口
     */
    @PostMapping("/sign")
    public BaseResponseDTO<?> sign(@RequestBody UserInfoDTO userInfoDTO){
        String telephone = userInfoDTO.getTelephone();
        String password = userInfoDTO.getPassword();
        String code = userInfoDTO.getCode();
        if (StringUtils.isEmpty(telephone)||StringUtils.isEmpty(password)||StringUtils.isEmpty(code)){
            return BaseResponseDTO.setResultError("手机号和密码不能为空");
        }
        boolean answer = sendSMS.verificationSMS(telephone, code, SMSEnum.USER_SIGN);
        if (!answer){
            return BaseResponseDTO.setResultError(HttpStatusCodeEM.SHORT_CODE_ERROR,"验证码错误");
        }
            return userService.userSign(userInfoDTO);



    }

    /**
     * 发送验证码
     * @param json telephone 要发送的手机号
     */
    @PostMapping("/code")
    public BaseResponseDTO<?> sendCode(@RequestBody JSONObject json){
        String telephone = json.getString("telephone");
        if (StringUtils.isEmpty(telephone) && telephone.length()!=11){
            return BaseResponseDTO.setResultError("手机号格式不正确");
        }
        String code = sendSMS.sendSMSToRedis(telephone, SMSEnum.USER_SIGN);
        return code==null? BaseResponseDTO.setResultError("验证码还在有效期内，请稍后发送"):BaseResponseDTO.setResultSuccess();
    }

}
